<?php
function plugin_render() {
    $units = ['b'=>1, 'B'=>8, 'KB'=>8*1024, 'MB'=>8*1024*1024, 'GB'=>8*1024*1024*1024, 'TB'=>8*1024*1024*1024*1024, 'PB'=>8*1024*1024*1024*1024*1024];
    $input = '';
    $from = 'B';
    $result = [];
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $input = floatval($_POST['input'] ?? 0);
        $from = $_POST['from'] ?? 'B';
        foreach ($units as $k => $v) {
            $result[$k] = $input * $units[$from] / $v;
        }
    }
    ?>
    <form method="post" class="space-y-4">
      <div class="flex gap-2">
        <input type="number" step="any" name="input" value="<?=htmlspecialchars($input)?>" class="border rounded px-3 py-2 w-40" placeholder="数值" required>
        <select name="from" class="border rounded px-3 py-2">
          <?php foreach ($units as $k=>$v): ?>
            <option value="<?=$k?>" <?php if($from===$k)echo'selected';?>><?=$k?></option>
          <?php endforeach; ?>
        </select>
        <button type="submit" class="bg-blue-500 text-white px-6 py-2 rounded">换算</button>
      </div>
    </form>
    <?php if ($result): ?>
      <div class="mt-4 grid grid-cols-2 sm:grid-cols-4 gap-4">
        <?php foreach ($result as $k=>$v): ?>
          <div class="bg-gray-50 border rounded p-2 text-center"><?=$k?>：<span class="font-bold"><?=round($v, 6)?></span></div>
        <?php endforeach; ?>
      </div>
    <?php endif;
} 